import { ref } from 'vue'
import { findTopCategory } from '@/api/category'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'

export default function useCategory () {
  const categroyAllList = ref([])
  const route = useRoute()
  // 设置默认参数为路由中的id
  async function getCategoryList (id = route.params.id) {
    const res = await findTopCategory(id)
    categroyAllList.value = res.result.children
  }
  getCategoryList()

  // 路由信息更新时自动执行的函数
  onBeforeRouteUpdate(to => {
    // 重新调用获取列表函数
    getCategoryList(to.params.id)
  })

  return { categroyAllList }
}
